package com.iwhalecloud.bss.kite.cucc.service.pos.bcmc.handler.impl;

import java.util.List;
import java.util.Objects;

import org.springframework.stereotype.Service;

import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.dto.pos.KitePosPaymentDTO;
import com.iwhalecloud.bss.kite.cucc.service.pos.bcmc.BcmcPosContext;
import com.iwhalecloud.bss.kite.cucc.service.util.ChangeTypeUtils;
import com.iwhalecloud.bss.kite.dataservice.cache.OfferAttrCache;
import com.iwhalecloud.bss.kite.dataservice.entity.OfferAttr;
import com.iwhalecloud.bss.kite.dataservice.util.IdUtils;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.enums.ActionType;
import com.iwhalecloud.bss.kite.manager.enums.InstType;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInst;
import com.ztesoft.bss.common.util.SpringUtil;

import edu.emory.mathcs.backport.java.util.Arrays;

@Service
public class BcmcPosPaymentHandler extends AbstractBcmcPosHandler {

    @Override
    public void doHandle(BcmcPosContext context) {
        KitePosPaymentDTO payment = new KitePosPaymentDTO();
        String instanceId = IdUtils.generateInstId(InstType.MKT_RES_INST);
        String changeType = ChangeTypeUtils.getChangeType(ActionType.A.name());
        // 签订任务需要用旧的实例id
        if (KiteListUtils.isNotEmpty(context.getPosDto().getPayments())) {
            instanceId = context.getPosDto().getPayments().get(0).getInstanceId();
            changeType = ChangeTypeUtils.getChangeType(ActionType.K.name());
        }
        payment.setInstanceId(instanceId);
        payment.setPayType("XCZF");
        payment.setPayWay("XJZF");

        payment.setPayStatus("0");
        payment.setChangeType(changeType);
        this.addInstanceIdAttr(context, instanceId, payment, changeType);

        KiteOfferInst mainOfferInst = null;
        IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);
        String mainOfferInstId = querySceneInstService.getExtParameterValue(context.getSceneInstId(),
            "mainOfferInstId");
        if (KiteStringUtils.isEmpty(mainOfferInstId)) {
            mainOfferInst = querySceneInstService.getMainOfferInst(context.getSceneInstId());
        }
        else {
            mainOfferInst = querySceneInstService.getOfferInst(context.getCustId(), mainOfferInstId);
        }

        List<OfferAttr> offerAttrs = OfferAttrCache.get(mainOfferInst.getOfferId());
        for (OfferAttr offerAttr : offerAttrs) {
            if ("price".equals(offerAttr.getFieldName())) {
                payment.setAmount(
                    (Double.valueOf(Objects.isNull(offerAttr.getDefaultValue()) ? "0" : offerAttr.getDefaultValue())
                        .longValue() * 1000));
                break;
            }
        }

        context.getPosDto().setPayments(Arrays.asList(new Object[] {
            payment
        }));

    }

}
